using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public class SqlPos_SalesMasterProvider:DataAccessObject
{
	public SqlPos_SalesMasterProvider()
    {
    }


    public bool DeletePos_SalesMaster(int pos_SalesMasterID)
    {
        using (SqlConnection connection = new SqlConnection(this.ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("GP_DeletePos_SalesMaster", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Pos_SalesMasterID", SqlDbType.Int).Value = pos_SalesMasterID;
            connection.Open();

            int result = cmd.ExecuteNonQuery();
            return (result == 1);
        }
    }

    public List<Pos_SalesMaster> GetAllPos_SalesMasters()
    {
        using (SqlConnection connection = new SqlConnection(this.ConnectionString))
        {
            SqlCommand command = new SqlCommand("GP_GetAllPos_SalesMasters", connection);
            command.CommandType = CommandType.StoredProcedure;
            connection.Open();
            IDataReader reader = command.ExecuteReader(CommandBehavior.Default);

            return GetPos_SalesMastersFromReader(reader);
        }
    }
    public List<Pos_SalesMaster> GetPos_SalesMastersFromReader(IDataReader reader)
    {
        List<Pos_SalesMaster> pos_SalesMasters = new List<Pos_SalesMaster>();

        while (reader.Read())
        {
            pos_SalesMasters.Add(GetPos_SalesMasterFromReader(reader));
        }
        return pos_SalesMasters;
    }

    public Pos_SalesMaster GetPos_SalesMasterFromReader(IDataReader reader)
    {
        try
        {
            Pos_SalesMaster pos_SalesMaster = new Pos_SalesMaster
                (
                    (int)reader["Pos_SalesMasterID"],
                    (int)reader["LocalSalesMasterID"],
                    (int)reader["InvoiceNo"],
                    (int)reader["WorkStationID"],
                    (DateTime)reader["SalesDate"],
                    (int)reader["SalesReturnID"],
                    (decimal)reader["CashSales"],
                    (int)reader["CashSalesJournalDetailID"],
                    (decimal)reader["DBBLAmount"],
                    (decimal)reader["CityAmount"],
                    (decimal)reader["VATPercentage"],
                    (decimal)reader["VATAmount"],
                    (decimal)reader["DiscountPercentage"],
                    (decimal)reader["DiscountAmount"],
                    (decimal)reader["RefundAmount"],
                    (int)reader["SalesPerson"],
                    (int)reader["Referrence"],
                    (int)reader["Customer"],
                    reader["MobileNo"].ToString(),
                    reader["CustomerName"].ToString(),
                    reader["Note"].ToString(),
                    (int)reader["LocalJournalMasterID"],
                    (int)reader["LocalTransactionMasterID"],
                    (int)reader["LocalReturnTransactionMasterID"],
                    (int)reader["ServerJournalMasterID"],
                    (int)reader["ServerTransactionMasterID"],
                    (int)reader["ServerReturnTransactionMasterID"],
                    (int)reader["StatusID"],
                    reader["ExtraField1"].ToString(),
                    reader["ExtraField2"].ToString(),
                    reader["ExtraField3"].ToString(),
                    reader["ExtraField4"].ToString(),
                    reader["ExtraField5"].ToString(),
                    reader["ExtraField6"].ToString(),
                    reader["ExtraField7"].ToString(),
                    reader["ExtraField8"].ToString(),
                    reader["ExtraField9"].ToString(),
                    reader["ExtraField10"].ToString()
                );
             return pos_SalesMaster;
        }
        catch(Exception ex)
        {
            return null;
        }
    }

    public Pos_SalesMaster GetPos_SalesMasterByID(int pos_SalesMasterID)
    {
        using (SqlConnection connection = new SqlConnection(this.ConnectionString))
        {
            SqlCommand command = new SqlCommand("GP_GetPos_SalesMasterByID", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@Pos_SalesMasterID", SqlDbType.Int).Value = pos_SalesMasterID;
            connection.Open();
            IDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow);

            if (reader.Read())
            {
                return GetPos_SalesMasterFromReader(reader);
            }
            else
            {
                return null;
            }
        }
    }

    public int InsertPos_SalesMaster(Pos_SalesMaster pos_SalesMaster)
    {
        using (SqlConnection connection = new SqlConnection(this.ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("GP_InsertPos_SalesMaster", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Pos_SalesMasterID", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.Parameters.Add("@LocalSalesMasterID", SqlDbType.Int).Value = pos_SalesMaster.LocalSalesMasterID;
            cmd.Parameters.Add("@InvoiceNo", SqlDbType.Int).Value = pos_SalesMaster.InvoiceNo;
            cmd.Parameters.Add("@WorkStationID", SqlDbType.Int).Value = pos_SalesMaster.WorkStationID;
            cmd.Parameters.Add("@SalesDate", SqlDbType.DateTime).Value = pos_SalesMaster.SalesDate;
            cmd.Parameters.Add("@SalesReturnID", SqlDbType.Int).Value = pos_SalesMaster.SalesReturnID;
            cmd.Parameters.Add("@CashSales", SqlDbType.Decimal).Value = pos_SalesMaster.CashSales;
            cmd.Parameters.Add("@CashSalesJournalDetailID", SqlDbType.Int).Value = pos_SalesMaster.CashSalesJournalDetailID;
            cmd.Parameters.Add("@DBBLAmount", SqlDbType.Decimal).Value = pos_SalesMaster.DBBLAmount;
            cmd.Parameters.Add("@CityAmount", SqlDbType.Decimal).Value = pos_SalesMaster.CityAmount;
            cmd.Parameters.Add("@VATPercentage", SqlDbType.Decimal).Value = pos_SalesMaster.VATPercentage;
            cmd.Parameters.Add("@VATAmount", SqlDbType.Decimal).Value = pos_SalesMaster.VATAmount;
            cmd.Parameters.Add("@DiscountPercentage", SqlDbType.Decimal).Value = pos_SalesMaster.DiscountPercentage;
            cmd.Parameters.Add("@DiscountAmount", SqlDbType.Decimal).Value = pos_SalesMaster.DiscountAmount;
            cmd.Parameters.Add("@RefundAmount", SqlDbType.Decimal).Value = pos_SalesMaster.RefundAmount;
            cmd.Parameters.Add("@SalesPerson", SqlDbType.Int).Value = pos_SalesMaster.SalesPerson;
            cmd.Parameters.Add("@Referrence", SqlDbType.Int).Value = pos_SalesMaster.Referrence;
            cmd.Parameters.Add("@Customer", SqlDbType.Int).Value = pos_SalesMaster.Customer;
            cmd.Parameters.Add("@MobileNo", SqlDbType.NVarChar).Value = pos_SalesMaster.MobileNo;
            cmd.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = pos_SalesMaster.CustomerName;
            cmd.Parameters.Add("@Note", SqlDbType.NText).Value = pos_SalesMaster.Note;
            cmd.Parameters.Add("@LocalJournalMasterID", SqlDbType.Int).Value = pos_SalesMaster.LocalJournalMasterID;
            cmd.Parameters.Add("@LocalTransactionMasterID", SqlDbType.Int).Value = pos_SalesMaster.LocalTransactionMasterID;
            cmd.Parameters.Add("@LocalReturnTransactionMasterID", SqlDbType.Int).Value = pos_SalesMaster.LocalReturnTransactionMasterID;
            cmd.Parameters.Add("@ServerJournalMasterID", SqlDbType.Int).Value = pos_SalesMaster.ServerJournalMasterID;
            cmd.Parameters.Add("@ServerTransactionMasterID", SqlDbType.Int).Value = pos_SalesMaster.ServerTransactionMasterID;
            cmd.Parameters.Add("@ServerReturnTransactionMasterID", SqlDbType.Int).Value = pos_SalesMaster.ServerReturnTransactionMasterID;
            cmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = pos_SalesMaster.StatusID;
            cmd.Parameters.Add("@ExtraField1", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField1;
            cmd.Parameters.Add("@ExtraField2", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField2;
            cmd.Parameters.Add("@ExtraField3", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField3;
            cmd.Parameters.Add("@ExtraField4", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField4;
            cmd.Parameters.Add("@ExtraField5", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField5;
            cmd.Parameters.Add("@ExtraField6", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField6;
            cmd.Parameters.Add("@ExtraField7", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField7;
            cmd.Parameters.Add("@ExtraField8", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField8;
            cmd.Parameters.Add("@ExtraField9", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField9;
            cmd.Parameters.Add("@ExtraField10", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField10;
            connection.Open();

            int result = cmd.ExecuteNonQuery();
            return (int)cmd.Parameters["@Pos_SalesMasterID"].Value;
        }
    }

    public bool UpdatePos_SalesMaster(Pos_SalesMaster pos_SalesMaster)
    {
        using (SqlConnection connection = new SqlConnection(this.ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("GP_UpdatePos_SalesMaster", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Pos_SalesMasterID", SqlDbType.Int).Value = pos_SalesMaster.Pos_SalesMasterID;
            cmd.Parameters.Add("@LocalSalesMasterID", SqlDbType.Int).Value = pos_SalesMaster.LocalSalesMasterID;
            cmd.Parameters.Add("@InvoiceNo", SqlDbType.Int).Value = pos_SalesMaster.InvoiceNo;
            cmd.Parameters.Add("@WorkStationID", SqlDbType.Int).Value = pos_SalesMaster.WorkStationID;
            cmd.Parameters.Add("@SalesDate", SqlDbType.DateTime).Value = pos_SalesMaster.SalesDate;
            cmd.Parameters.Add("@SalesReturnID", SqlDbType.Int).Value = pos_SalesMaster.SalesReturnID;
            cmd.Parameters.Add("@CashSales", SqlDbType.Decimal).Value = pos_SalesMaster.CashSales;
            cmd.Parameters.Add("@CashSalesJournalDetailID", SqlDbType.Int).Value = pos_SalesMaster.CashSalesJournalDetailID;
            cmd.Parameters.Add("@DBBLAmount", SqlDbType.Decimal).Value = pos_SalesMaster.DBBLAmount;
            cmd.Parameters.Add("@CityAmount", SqlDbType.Decimal).Value = pos_SalesMaster.CityAmount;
            cmd.Parameters.Add("@VATPercentage", SqlDbType.Decimal).Value = pos_SalesMaster.VATPercentage;
            cmd.Parameters.Add("@VATAmount", SqlDbType.Decimal).Value = pos_SalesMaster.VATAmount;
            cmd.Parameters.Add("@DiscountPercentage", SqlDbType.Decimal).Value = pos_SalesMaster.DiscountPercentage;
            cmd.Parameters.Add("@DiscountAmount", SqlDbType.Decimal).Value = pos_SalesMaster.DiscountAmount;
            cmd.Parameters.Add("@RefundAmount", SqlDbType.Decimal).Value = pos_SalesMaster.RefundAmount;
            cmd.Parameters.Add("@SalesPerson", SqlDbType.Int).Value = pos_SalesMaster.SalesPerson;
            cmd.Parameters.Add("@Referrence", SqlDbType.Int).Value = pos_SalesMaster.Referrence;
            cmd.Parameters.Add("@Customer", SqlDbType.Int).Value = pos_SalesMaster.Customer;
            cmd.Parameters.Add("@MobileNo", SqlDbType.NVarChar).Value = pos_SalesMaster.MobileNo;
            cmd.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = pos_SalesMaster.CustomerName;
            cmd.Parameters.Add("@Note", SqlDbType.NText).Value = pos_SalesMaster.Note;
            cmd.Parameters.Add("@LocalJournalMasterID", SqlDbType.Int).Value = pos_SalesMaster.LocalJournalMasterID;
            cmd.Parameters.Add("@LocalTransactionMasterID", SqlDbType.Int).Value = pos_SalesMaster.LocalTransactionMasterID;
            cmd.Parameters.Add("@LocalReturnTransactionMasterID", SqlDbType.Int).Value = pos_SalesMaster.LocalReturnTransactionMasterID;
            cmd.Parameters.Add("@ServerJournalMasterID", SqlDbType.Int).Value = pos_SalesMaster.ServerJournalMasterID;
            cmd.Parameters.Add("@ServerTransactionMasterID", SqlDbType.Int).Value = pos_SalesMaster.ServerTransactionMasterID;
            cmd.Parameters.Add("@ServerReturnTransactionMasterID", SqlDbType.Int).Value = pos_SalesMaster.ServerReturnTransactionMasterID;
            cmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = pos_SalesMaster.StatusID;
            cmd.Parameters.Add("@ExtraField1", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField1;
            cmd.Parameters.Add("@ExtraField2", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField2;
            cmd.Parameters.Add("@ExtraField3", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField3;
            cmd.Parameters.Add("@ExtraField4", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField4;
            cmd.Parameters.Add("@ExtraField5", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField5;
            cmd.Parameters.Add("@ExtraField6", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField6;
            cmd.Parameters.Add("@ExtraField7", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField7;
            cmd.Parameters.Add("@ExtraField8", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField8;
            cmd.Parameters.Add("@ExtraField9", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField9;
            cmd.Parameters.Add("@ExtraField10", SqlDbType.NVarChar).Value = pos_SalesMaster.ExtraField10;
            connection.Open();

            int result = cmd.ExecuteNonQuery();
            return result == 1;
        }
    }
}
